<?php
/**
 * Created by PhpStorm.
 * User: yuliang
 * Date: 2017/10/30
 * Time: 下午6:27
 */
namespace app\webapi\controller;

use app\common\controller\Api;
use app\behavior;
use think\Cache;
use think\Cookie;
use think\Db;
use think\Request;
use app\common;

class Auth extends Api{


    public function sns(){
//        $str = 'a:2:{s:4:"data";a:6:{s:11:"screen_name";s:12:"木名解说";s:7:"open_id";i:935770320008;s:12:"access_token";s:36:"73a486cdee287e206b972cf6188a372e0008";s:10:"expires_in";i:1516688299;s:8:"uid_type";i:12;s:3:"uid";i:50774708546;}s:3:"ret";i:0;}';
//        $str = unserialize($str);
//        var_dump($str);die;
        $error_code = Request::instance()->has('error_code','get');

        if($error_code){
            exit();
        }else{
            $config = config('platform');
            $client_key = $config['sns_key'];
            $client_secret = $config['sns_secret'];
            $model = model('Account');

            $data['platform'] = 1;
            $data['platform_name'] = '头条号开放平台';
            $data['account'] = '';
            $data['password'] = '';
            $data['uid'] = input('state');
            $code = input('code');
            $url = 'https://open.snssdk.com/auth/token/?code='.$code.'&client_key='.$client_key.'&client_secret='.$client_secret.'&grant_type=authorize_code';
            $result = doCurl($url);

            if($result['ret'] == 0){
                // 账号
                $data['account'] = $result['data']['screen_name'];
//                $data['result'] = serialize($result);
                $data['result'] = json_encode($result);
                $data['status'] = 1;
                // 判断是否已存在账号信息
                $map['uid'] = $data['uid'];
                $map['platform'] = $data['platform'];
                $has = $model->where($map)->find();
                if($has){
                    $data['update_time'] = date('Y-m-d H:i:s',time());
                    $result = $model->isUpdate(true)->save($data,$map);
                }else{
                    $data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s',time());
                    $result = $model->isUpdate(false)->save($data);
                }
            }else{
                $data['status'] = 2;
            }
        }
        return $this->fetch('sns');
//        $this->display();
    }

    public function om(){
        $model = model('Account');
        $config = config('platform');
        $client_key = $config['om_key'];
        $client_secret = $config['om_secret'];

        $data['platform'] = 4;
        $data['platform_name'] = '企鹅号开放平台';
        $data['account'] = '';
        $data['password'] = '';
        $data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s', time());
        $data['uid'] = input('state');
        $code = input('code');

        $curl['code'] = $code;
        $curl['client_id'] = $client_key;
        $curl['client_secret'] = $client_secret;
        $curl['grant_type'] = 'authorization_code';
//        $url = 'https://auth.om.qq.com/omoauth2/accesstoken?grant_type=authorization_code&client_id=' . $client_key . '&client_secret=' . $client_secret . '&code=' . $code;
        $url = 'https://auth.om.qq.com/omoauth2/accesstoken';
        $result = doCurl($url,null,$curl);
        $data['result'] = json_encode($result);
        $data['status'] = 1;
        // 判断是否已存在账号信息
        $map['uid'] = $data['uid'];
        $map['platform'] = $data['platform'];
        $has = $model->where($map)->find();
        if ($has) {
            $data['update_time'] = date('Y-m-d H:i:s', time());
            $result = $model->isUpdate(true)->save($data, $map);
        } else {
            $data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s', time());
            $result = $model->isUpdate(false)->save($data);
        }
        return $this->fetch('sns');

    }

    public function iqiyi(){

        $config = config('platform');
        $client_key = $config['iqiyi_key'];
        $client_secret = $config['iqiyi_secret'];
        $model = model('Account');

        $data['platform'] = 3;
        $data['platform_name'] = '爱奇艺';
        $data['account'] = '';
        $data['password'] = '';
        $data['uid'] = input('state');
        $code = input('code');
        $url = 'https://openapi.iqiyi.com/api/oauth2/token?client_id='.$client_key.'&code='.$code.'&grant_type=authorization_code';
        $result = doCurl($url);

        if($result['code'] == 'A00000'){
            $data['result'] = json_encode($result);
            $data['status'] = 1;
            // 判断是否已存在账号信息
            $map['uid'] = $data['uid'];
            $map['platform'] = $data['platform'];
            $has = $model->where($map)->find();
            if($has){
                $data['update_time'] = date('Y-m-d H:i:s',time());
                $result = $model->isUpdate(true)->save($data,$map);
            }else{
                $data['create_time'] = $data['update_time'] = date('Y-m-d H:i:s',time());
                $result = $model->isUpdate(false)->save($data);
            }
        }else{
            $data['status'] = 2;
        }
        return $this->fetch('sns');
    }

    // 根据平台ID获取平台分类信息
    public function platformSort(){
        $platform = input('get.platform')?input('get.platform'):1;
        switch ($platform){
            case 1:
                $key = 'sns';
                $data = Cache::get('sns');
                break;
            case 3:
                $key = 'iqiyi';
                $data = Cache::get('iqiyi');
                break;
            case 4:
                $key = 'om';
                $data = Cache::get('om');
                break;
        }

        if(!$data){
            $result = Db::name('sort')->where("platform = $platform")->select();
            $data = make_tree($result);
            Cache::set($key,$data);
        }
        return api_return('操作成功!',$data);
    }
}
